package com.example.mypplication.dispatchersio

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mypplication.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class DispatchersIOActivity : AppCompatActivity(), CoroutineScope by MainScope() {

    companion object{
        lateinit var instance: Context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_dispatchers_ioactivity)
//        instance = this
//        initDisptachers()

        multiCoroutine()

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private var tmpValue = 0

    private fun multiCoroutine() {
//        launch {
//            repeat(100){
//                launch {
//                    delay(10)
//                    tmpValue++
//                    println("多协程结果：$tmpValue")
//                }
//            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
            launch {
                delay(10)
                tmpValue++
                println("多协程结果：$tmpValue")
            }
//            delay(1000)
//            println("多协程最后结果：$tmpValue")
//        }
    }

    private fun initDisptachers() {
        launch(IO){
            while(true){
                Log.d("测试内存泄漏", "打印日志中。。。")
                delay(1000)
            }
        }
    }

    override fun onDestroy() {
        cancel()
        super.onDestroy()
    }
}